package com.cuitrip.business.share;


import android.content.Context;

import com.cuitrip.business.login.proxy.ThirdAccountProxy;
import com.cuitrip.business.share.model.ServiceShareObject;
import com.cuitrip.business.share.model.Share;
import com.cuitrip.business.share.model.ShareParamBuilder;
import com.cuitrip.business.webview.H5UrlMaker;
import com.cuitrip.service.R;
import com.cuitrip.util.I118Util;
import com.cuitrip.util.ResourceUtil;
import com.lab.rtenvironment.AppCoreRuntime;
import com.umeng.onlineconfig.OnlineConfigAgent;

import java.util.ArrayList;
import java.util.List;

import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.system.email.Email;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * 分享内容的组合
 * Created by ak
 */
public class ShareChannelBuilder {

    public static final String INVITE_FRIEND_PIC = "http://www.tripinsiders.com/static/img/common/index-qr-code.png";


    public static final TripShareChannel[] simplifyChineseShareChannelArray = new TripShareChannel[]{TripShareChannel.TITLE, TripShareChannel.FACEBOOK, TripShareChannel.MOMMENTS, TripShareChannel.WEIBO, TripShareChannel.WECHAT, TripShareChannel.QQ, TripShareChannel.QQZONE, TripShareChannel.MAIL, TripShareChannel.COPY};

    public static final TripShareChannel[] exSimplifyChineseShareChannelArray = new TripShareChannel[]{TripShareChannel.TITLE, TripShareChannel.FACEBOOK, TripShareChannel.MAIL, TripShareChannel.COPY};


    public static List<TripShareChannel> inflateInviteSimpleShareList() {
        List<TripShareChannel> tripShareChannels = new ArrayList<>();
        if (I118Util.isSimplifiedChinese()) {
            addShareChannel(tripShareChannels, simplifyChineseShareChannelArray);
            if (tripShareChannels.size() > 4) {
                tripShareChannels = tripShareChannels.subList(0, 4);
                tripShareChannels.add(TripShareChannel.More);
            }
        } else {
            addShareChannel(tripShareChannels, exSimplifyChineseShareChannelArray);
        }
        return tripShareChannels;
    }

    public static int addMoreInviteShareList(List<TripShareChannel> shareList) {
        int i = 0;
        if (I118Util.isSimplifiedChinese()) {
            for (TripShareChannel tripShareChannel : simplifyChineseShareChannelArray) {
                if (!shareList.contains(tripShareChannel)) {
                    addShareChannel(shareList, tripShareChannel);
                    i++;
                }
            }
        }
        return i;
    }

    public static boolean getFacebookFlag(Context context) {
        String value = OnlineConfigAgent.getInstance().getConfigParams(context, "facebook_share");
        return "1".equalsIgnoreCase(value);
    }

    public static List<TripShareChannel> addShareChannel(List<TripShareChannel> shareList, TripShareChannel... tripShareChannelList) {
        for (TripShareChannel tripShareChannel : tripShareChannelList) {
            if (!channelValid(tripShareChannel)) {
                continue;
            }
            shareList.add(tripShareChannel);
        }
        return shareList;
    }

    private static boolean channelValid(TripShareChannel tripShareChannel) {
        //微信分享判断
        if (tripShareChannel.ordinal() == TripShareChannel.WECHAT.ordinal() || tripShareChannel.ordinal() == TripShareChannel.MOMMENTS.ordinal()) {
            return ThirdAccountProxy.isWechatInstall(AppCoreRuntime.context);
        }

        //qq分享判断
        if (tripShareChannel.ordinal() == TripShareChannel.QQ.ordinal()) {
            return ThirdAccountProxy.isQQInstalled(AppCoreRuntime.context);
        }

        //QQ空间分享判断
        if (tripShareChannel.ordinal() == TripShareChannel.QQZONE.ordinal()) {
            return ThirdAccountProxy.isQQInstalled(AppCoreRuntime.context);
        }

        //facebook分享判断
        if (tripShareChannel.ordinal() == TripShareChannel.FACEBOOK.ordinal()) {
            return getFacebookFlag(AppCoreRuntime.context);
        }

        return true;
    }

    public enum TripShareChannel {

        TITLE {
            @Override
            public Share getServiceShare(ServiceShareObject shareObject) {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share titleShare = builder.setMedia(Share.TITLE_ACTION)
                        .build();
                return titleShare;
            }

            @Override
            public Share addInviteShare() {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share titleShare = builder.setMedia(Share.TITLE_ACTION)
                        .build();
                return titleShare;
            }
        },

        WECHAT {
            @Override
            public Share getServiceShare(ServiceShareObject shareObject) {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share wechatShare = builder.setMedia(Wechat.NAME)
                        .setTitle(shareObject.getName())
                        .setContent(shareObject.getDescription())
                        .setImgUrl(shareObject.getPic())
                        .setDetailUrl(shareObject.getUrl())
                        .build();
                return wechatShare;
            }

            @Override
            public Share addInviteShare() {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share wechatShare = builder.setMedia(Wechat.NAME)
                        .setTitle(ResourceUtil.getString(R.string.share_title))
                        .setContent(ResourceUtil.getString(R.string.share_desc))
                        .setImgUrl(INVITE_FRIEND_PIC)
                        .setDetailUrl(H5UrlMaker.getShareFriendUrl())
                        .build();
                return wechatShare;
            }
        },

        MOMMENTS {
            @Override
            public Share getServiceShare(ServiceShareObject shareObject) {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share wechatMommentShare = builder.setMedia(WechatMoments.NAME)
                        .setTitle(shareObject.getName())
                        .setContent(shareObject.getName())
                        .setImgUrl(shareObject.getPic())
                        .setDetailUrl(shareObject.getUrl())
                        .build();
                return wechatMommentShare;
            }

            @Override
            public Share addInviteShare() {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share wechatMommentShare = builder.setMedia(WechatMoments.NAME)
                        .setTitle(ResourceUtil.getString(R.string.share_title))
                        .setContent(ResourceUtil.getString(R.string.share_desc))
                        .setImgUrl(INVITE_FRIEND_PIC)
                        .setDetailUrl(H5UrlMaker.getShareFriendUrl())
                        .build();
                return wechatMommentShare;
            }
        },
        WEIBO {
            @Override
            public Share getServiceShare(ServiceShareObject shareObject) {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share weiboShare = builder.setMedia(SinaWeibo.NAME)
                        .setTitle("")
                        .setContent(shareObject.getName() + shareObject.getUrl())
//                        .setDetailUrl(shareObject.getUrl())
                        .setImgUrl(shareObject.getPic())
                        .build();
                return weiboShare;
            }

            @Override
            public Share addInviteShare() {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share weiboShare = builder.setMedia(SinaWeibo.NAME)
                        .setTitle("")
                        .setContent(ResourceUtil.getString(R.string.share_title) + ResourceUtil.getString(R.string.share_from) + H5UrlMaker.getShareFriendUrl())
//                        .setDetailUrl(H5UrlMaker.getShareFriendUrl())
                        .setImgUrl(INVITE_FRIEND_PIC)
                        .build();
                return weiboShare;
            }
        },
        QQ {
            @Override
            public Share getServiceShare(ServiceShareObject shareObject) {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share qqShare = builder.setMedia(cn.sharesdk.tencent.qq.QQ.NAME)
                        .setTitle(shareObject.getName())
                        .setContent(shareObject.getShortDescription())
                        .setImgUrl(shareObject.getPic())
                        .setDetailUrl(shareObject.getUrl())
                        .build();
                return qqShare;
            }

            @Override
            public Share addInviteShare() {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share qqShare = builder.setMedia(cn.sharesdk.tencent.qq.QQ.NAME)
                        .setTitle(ResourceUtil.getString(R.string.share_title))
                        .setContent(ResourceUtil.getString(R.string.share_desc))
                        .setImgUrl(INVITE_FRIEND_PIC)
                        .setDetailUrl(H5UrlMaker.getShareFriendUrl())
                        .build();
                return qqShare;
            }

        },

        QQZONE {
            @Override
            public Share getServiceShare(ServiceShareObject shareObject) {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share qzoneShare = builder.setMedia(QZone.NAME)
                        .setTitle(shareObject.getName())
                        .setContent(shareObject.getShortDescription())
                        .setImgUrl(shareObject.getPic())
                        .setDetailUrl(shareObject.getUrl())
                        .build();
                return qzoneShare;
            }

            @Override
            public Share addInviteShare() {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share qzoneShare = builder.setMedia(QZone.NAME)
                        .setTitle(ResourceUtil.getString(R.string.share_title))
                        .setContent(ResourceUtil.getString(R.string.share_desc))
                        .setImgUrl(INVITE_FRIEND_PIC)
                        .setDetailUrl(H5UrlMaker.getShareFriendUrl())
                        .build();
                return qzoneShare;
            }
        },

        MAIL {
            @Override
            public Share getServiceShare(ServiceShareObject shareObject) {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share mailShare = builder.setMedia(Email.NAME)
                        .setTitle(ResourceUtil.getString(R.string.share_at_user, shareObject.getName()))
                        .setContent(shareObject.getLongDescription() + "<br/>" + ResourceUtil.getString(R.string.operation_view_detail) + ": " + shareObject.getUrl())
                        .setImgUrl(shareObject.getPic())
                        .setDetailUrl(shareObject.getUrl())
                        .build();
                return mailShare;
            }

            @Override
            public Share addInviteShare() {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share mailShare = builder.setMedia(Email.NAME)
                        .setTitle(String.format(ResourceUtil.getString(R.string.share_at_user), ResourceUtil.getString(R.string.share_title)))
                        .setContent(ResourceUtil.getString(R.string.share_desc) + "<br/>" +
                                ResourceUtil.getString(R.string.share_download, H5UrlMaker.getShareFriendUrl()) + "<br/>" +
                                ResourceUtil.getString(R.string.share_official_account))
                        .setImgUrl(INVITE_FRIEND_PIC)
                        .setDetailUrl(H5UrlMaker.getShareFriendUrl())
                        .build();
                return mailShare;
            }
        },

        FACEBOOK {
            @Override
            public Share getServiceShare(ServiceShareObject shareObject) {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share facebookShare = builder.setMedia(Facebook.NAME)
                        .setTitle(shareObject.getName())
                        .setContent(shareObject.getShortDescription() + shareObject.getUrl())
                        .setImgUrl(shareObject.getPic())
                        .setImgPath(shareObject.getImagePath())
                        .setDetailUrl(shareObject.getUrl())
                        .build();
                return facebookShare;
            }


            @Override
            public Share addInviteShare() {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share facebookShare = builder.setMedia(Facebook.NAME)
                        .setTitle(ResourceUtil.getString(R.string.share_title))
                        .setContent(ResourceUtil.getString(R.string.share_desc))
                        .setDetailUrl(H5UrlMaker.getShareFriendUrl())
                        .setImgUrl(INVITE_FRIEND_PIC)
                        .build();
                return facebookShare;
            }
        },
        TWITTER {
        },
        LINE {

        },
        COPY {
            @Override
            public Share getServiceShare(ServiceShareObject shareObject) {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share copyShare = builder.setMedia(Share.COPY_ACTION)
                        .setTitle(ResourceUtil.getString(R.string.share_copy))
                        .setDetailUrl(shareObject.getUrl())
                        .build();
                return copyShare;
            }

            @Override
            public Share addInviteShare() {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share copyShare = builder.setMedia(Share.COPY_ACTION)
                        .setTitle(ResourceUtil.getString(R.string.share_copy))
                        .setDetailUrl(H5UrlMaker.getShareFriendUrl())
                        .build();
                return copyShare;
            }
        },
        More {
            @Override
            public Share getServiceShare(ServiceShareObject shareObject) {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share moreShare = builder.setMedia(Share.MORE_ACTION)
                        .build();
                return moreShare;
            }

            @Override
            public Share addInviteShare() {
                ShareParamBuilder builder = new ShareParamBuilder();
                Share moreShare = builder.setMedia(Share.MORE_ACTION)
                        .build();
                return moreShare;
            }
        };

        public static final int TRIP_TYPE = 0x01;
        public static final int INVITE_TYPE = 0x02;

        public Share getServiceShare(ServiceShareObject shareObject) {
            return null;
        }

        public Share addInviteShare() {
            return null;
        }
    }

    public static Share getShareObject(int shareType, TripShareChannel tripShareChannel, ServiceShareObject shareObject) {
        if (shareType == TripShareChannel.INVITE_TYPE) {
            return tripShareChannel.addInviteShare();
        } else {
            return tripShareChannel.getServiceShare(shareObject);
        }
    }


}
